Fruits into Baskets (medium)
We'll cover the following
Problem Statement #
Given an array of characters where each character represents a fruit tree, you are given two baskets and your goal is to put maximum number of fruits in each basket. The only restriction is that each basket can have only one type of fruit.
You can start with any tree, but once you have started you canāt skip a tree. You will pick one fruit from each tree until you cannot, i.e., you will stop when you have to pick from a third fruit type.
Write a function to return the maximum number of fruits in both the baskets.
Example 1:
Input: Fruit=['A', 'B', 'C', 'A', 'C']
Output: 3
Explanation: We can put 2 'C' in one basket and one 'A' in the other from the subarray ['C', 'A', 'C']
Example 2:
Input: Fruit=['A', 'B', 'C', 'B', 'B', 'C']
Output: 5
Explanation: We can put 3 'B' in one basket and two 'C' in the other basket.
This can be done if we start with the second letter: ['B', 'C', 'B', 'B', 'C']
Try it yourself #
Try solving this question here:
Solution #
This problem follows the Sliding Window pattern and is quite similar to Longest Substring with K Distinct Characters. In this problem, we need to find the length of the longest subarray with no more than two distinct characters (or fruit types!). This transforms the current problem into Longest Substring with K Distinct Characters where K=2.
Code #
Here is what our algorithm will look like, only the highlighted lines are different from Longest Substring with K Distinct Characters:
Time Complexity #
The time complexity of the above algorithm will be where āNā is the number of characters in the input array. The outer for
loop runs for all characters and the inner while
loop processes each character only once, therefore the time complexity of the algorithm will be which is asymptotically equivalent to .
Space Complexity #
The algorithm runs in constant space as there can be a maximum of three types of fruits stored in the frequency map.
Similar Problems #
Problem 1: Longest Substring with at most 2 distinct characters
Given a string, find the length of the longest substring in it with at most two distinct characters.
Solution: This problem is exactly similar to our parent problem.